package mashuaiproject.mashuaiproject.controller;

import mashuaiproject.mashuaiproject.dto.AddFriendsUser;
import mashuaiproject.mashuaiproject.dto.GuanzhuUsersDto;
import mashuaiproject.mashuaiproject.dto.OtherMesDto;
import mashuaiproject.mashuaiproject.dto.SaveUsersQuesionDato;
import mashuaiproject.mashuaiproject.mapper.AttributeMappper;
import mashuaiproject.mashuaiproject.mapper.OtherMapper;
import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.Dongtai;
import mashuaiproject.mashuaiproject.model.GuanZhu;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.model.XunZhang;
import mashuaiproject.mashuaiproject.serivce.GuanZhuUserSerivce;
import mashuaiproject.mashuaiproject.serivce.JMontiorSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SelfController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    JavaMailSender mailSender;
    @Autowired
    private  OtherMapper otherMapper;
    @Autowired
    private  GuanZhuUserSerivce guanZhuUserSerivce;
    @Autowired
    private JMontiorSerivce jMontiorSerivce;
    @GetMapping("/selfs")
    public  String  selfs(Model model,
                          HttpServletRequest request,
                          @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                          @RequestParam(value = "size",required = false,defaultValue = "6") Integer size ,
                          @RequestParam(value="tags",required = false,defaultValue = "myGuanzhu")String tags,
                          @RequestParam(value="del",required = false,defaultValue = "0")Integer del,
                          @RequestParam(value="delMyGuanzhu",required = false,defaultValue = "0")Integer delMyGuanzhu){
        //查找出用户的信息
        Users users= (Users) request.getSession().getAttribute("user");
        //这是取消受仓
        if(del!=0){
            //删除文章
            int delSave=guanZhuUserSerivce.delSaves(users.getId(),del);
            //动态监测
            jMontiorSerivce.checkHasGuaZhu(users.getId(),del,"saveReds");
            jMontiorSerivce.Upleave(users.getId());
            Integer insert =jMontiorSerivce.insertDongtai(users.getId(),6,del);//用户id，动态类型，不集体类型

        }
        //取消关注
        if(delMyGuanzhu!=0){
            int deguazhu=guanZhuUserSerivce.delMyGuanzhus(users.getId(),delMyGuanzhu);
            //监测行为
            Integer insert =jMontiorSerivce.insertDongtai(users.getId(),4,delMyGuanzhu);//用户id，动态类型，不集体类型

        }
        //查找出用户的关注人
        List<GuanzhuUsersDto> myguanzhu=guanZhuUserSerivce.selectMyGuanzhu(users.getId(),1,size);

        //查找该用户粉丝信息
       List<GuanzhuUsersDto> guanzhuUsersDtoList=guanZhuUserSerivce.selectGuanzhuUserMes(users,1,size,tags);//默认显示的信息
       //查找吃出这个用户收藏的文章
        List<SaveUsersQuesionDato> saveUsersQuesionDatoes=guanZhuUserSerivce.selectSaveUsersQuesionDatoes(users.getId(),1,size,tags);

        //判断那个选项卡定位到哪个内容
        if(tags!=null){
            if(tags.equals("loveQuesions")){
                saveUsersQuesionDatoes=guanZhuUserSerivce.selectSaveUsersQuesionDatoes(users.getId(),page,size,tags);
                model.addAttribute("activeshow","loveQuesions");
            }
            if(tags.equals("myFensi")){
                 guanzhuUsersDtoList=guanZhuUserSerivce.selectGuanzhuUserMes(users,page,size,tags);

                model.addAttribute("activeshow","myFensi");
            }
            if(tags.equals("myGuanzhu")){
                myguanzhu=guanZhuUserSerivce.selectMyGuanzhu(users.getId(),page,size);
                model.addAttribute("activeshow","myGuanzhu");
            }
            if(tags.equals("recentDyamics")){
                model.addAttribute("activeshow","recentDyamics");
            }
            if(tags.equals("myFriends")){
                model.addAttribute("activeshow","myFriends");
            }
        }
        //携带勋章
        List<OtherMesDto> otherMesDtos=guanZhuUserSerivce.selectAllXunZhang(users.getId());
        //携带当前的动态
        List<Dongtai> dongtais=guanZhuUserSerivce.selectAllDongtai(users.getId());
        //查找申请人好友信息
        List<AddFriendsUser> friendsUserList=guanZhuUserSerivce.selectOneFriedn(users.getId());
        //好友申请我的信息
        model.addAttribute("myguanzhu",myguanzhu);
        model.addAttribute("user",users);
        model.addAttribute("saveUsersQuesionDatoes",saveUsersQuesionDatoes);
        model.addAttribute("guanzhuUsersDtoList",guanzhuUsersDtoList);
        model.addAttribute("otherMesDtos3",otherMesDtos);
        model.addAttribute("dongtais",dongtais);
        model.addAttribute("friendsUserList",friendsUserList);//好友
        return "self";
    }


    //修改签名
    @ResponseBody
    @GetMapping("/modifeSignature")
    public  String  modifeSignature(@RequestParam("signature")String signature,
                                    HttpServletRequest request){
        Users users= (Users) request.getSession().getAttribute("user");
        int update=userMapper.upateSignature(users.getId(),signature);
        if(update!=0){
            return "succes";
        }
        return "failed";
    }


    //修改姓名
    @ResponseBody
    @GetMapping("/modifiedName")
    public String modifiedName(@RequestParam("modifiedName")String modifiedName,
             HttpServletRequest request){
        //首先判断用户名是否已经存在
        int checkName=userMapper.checkName(modifiedName);
        if(checkName!=0){
            return "failed";
        }else{
            Users users= (Users) request.getSession().getAttribute("user");
            userMapper.updateName(users.getId(),modifiedName);
            return "succes";
        }

    }

    //查看密码或者修改密码：
    @ResponseBody
    @GetMapping("/lookPass")
    public Map<String ,Object> lookPass(HttpServletRequest request){
        Map<String ,Object> map=new HashMap<>();
        Users users= (Users) request.getSession().getAttribute("user");
        //获取当前用户的密码
        try {
            final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom("937958191@qq.com");
            message.setTo(users.getAccount_id());
            message.setSubject("查看密码或者修改密码：");
            message.setText("你的账号为："+users.getAccount_id()+",密码："+users.getPassword()+"。如果需要修改密码，请点击http://119.3.179.130:8087/modified/"+users.getId()+"");
            this.mailSender.send(mimeMessage);
            map.put("vectiy",users.getPassword());
            map.put("user",users);

        } catch (Exception ex) {
            map.put("vectiy",null);
            ex.printStackTrace();
        }


        return map;
    }

    //查看勋章领取系信息
    @ResponseBody
    @GetMapping("/getXunzhangMes")
    public  Map<String ,Object> getXunzhangMes(@RequestParam("xunzhangId")Integer xunzhangId,
                                               @RequestParam("isGetXunzhang")String  isGetXunzhang,
                                               HttpServletRequest request){
        //找出当胸章的信息
        Users users= (Users) request.getSession().getAttribute("user");
        Map<String ,Object> map=guanZhuUserSerivce.selectAllmes(xunzhangId,isGetXunzhang, users.getId());

        //查看这个胸章有多少人领取
        return map;
    }
    @ResponseBody
    @GetMapping("/getXZ")
    public  String  getXZs(@RequestParam("xzId") Integer xzId,HttpServletRequest request){
        Users users= (Users) request.getSession().getAttribute("user");
        //插入该胸章
        Integer insetrXZ=guanZhuUserSerivce.insertXZ(users.getId(),xzId);

        //动态监测
        //根据勋章id找到勋章；
        XunZhang xzname=otherMapper.selectJudegXUnzhng(xzId);
        String  object=users.getName()+",领取了:"+xzname.getName();
        Integer insert =jMontiorSerivce.insertDongtai(users.getId(),7,object);//用户id，动态类型，不集体类型


        return "scuess";
    }

}
